﻿/* 'app' MODULE DEFINITION */
var appModule = angular.module("app", [
    "ui.router",
    "ui.bootstrap",
    'ui.utils',
    "ui.jq",
    'ui.grid',
    'ui.grid.pagination',
    "oc.lazyLoad",
    "ngSanitize",
    'angularFileUpload',
    'daterangepicker',
    'angularMoment',
    'frapontillo.bootstrap-switch',
    'abp',
     "kendo.directives"
]);

/* LAZY LOAD CONFIG */

/* This application does not define any lazy-load yet but you can use $ocLazyLoad to define and lazy-load js/css files.
 * This code configures $ocLazyLoad plug-in for this application.
 * See it's documents for more information: https://github.com/ocombe/ocLazyLoad
 */
appModule.config(['$ocLazyLoadProvider', function ($ocLazyLoadProvider) {
    $ocLazyLoadProvider.config({
        cssFilesInsertBefore: 'ng_load_plugins_before', // load the css files before a LINK element with this ID.
        debug: false,
        events: true,
        modules: []
    });
}]);

/* THEME SETTINGS */
App.setAssetsPath(abp.appPath + 'metronic/assets/');
appModule.factory('settings', ['$rootScope', function ($rootScope) {
    var settings = {
        layout: {
            pageSidebarClosed: false, // sidebar menu state
            pageContentWhite: true, // set page content layout
            pageBodySolid: false, // solid body color state
            pageAutoScrollOnLoad: 1000 // auto scroll to top on page load
        },
        layoutImgPath: App.getAssetsPath() + 'admin/layout4/img/',
        layoutCssPath: App.getAssetsPath() + 'admin/layout4/css/',
        assetsPath: abp.appPath + 'metronic/assets',
        globalPath: abp.appPath + 'metronic/assets/global',
        layoutPath: abp.appPath + 'metronic/assets/layouts/layout4'
    };

    $rootScope.settings = settings;

    return settings;
}]);

/* ROUTE DEFINITIONS */

appModule.config([
    '$stateProvider', '$urlRouterProvider',
    function ($stateProvider, $urlRouterProvider) {

        // Default route (overrided below if user has permission)设置站点访问的默认地址
        $urlRouterProvider.otherwise("/welcome");
        //Welcome page(欢迎界面)
        $stateProvider.state('welcome', {
            url: '/welcome',
            templateUrl: '~/App/common/views/welcome/index.cshtml'
        });

        //COMMON routes
        //角色
        if (abp.auth.hasPermission('Pages.Administration.Roles')) {
            $stateProvider.state('roles', {
                url: '/roles',
                templateUrl: '~/App/common/views/roles/index.cshtml',
                menu: 'Administration.Roles'
            });
        }
        //用户
        if (abp.auth.hasPermission('Pages.Administration.Users')) {
            $stateProvider.state('users', {
                url: '/users?filterText',
                templateUrl: '~/App/common/views/users/index.cshtml',
                menu: 'Administration.Users'
            });
        }
        //语言
        if (abp.auth.hasPermission('Pages.Administration.Languages')) {
            //语言列表
            $stateProvider.state('languages', {
                url: '/languages',
                templateUrl: '~/App/common/views/languages/index.cshtml',
                menu: 'Administration.Languages'
            });
            //语言的文本信息
            if (abp.auth.hasPermission('Pages.Administration.Languages.ChangeTexts')) {
                $stateProvider.state('languageTexts', {
                    url: '/languages/texts/:languageName?sourceName&baseLanguageName&targetValueFilter&filterText',
                    templateUrl: '~/App/common/views/languages/texts.cshtml',
                    menu: 'Administration.Languages'
                });
            }
        }
        //审计日志
        if (abp.auth.hasPermission('Pages.Administration.AuditLogs')) {
            $stateProvider.state('auditLogs', {
                url: '/auditLogs',
                templateUrl: '~/App/common/views/auditLogs/index.cshtml',
                menu: 'Administration.AuditLogs'
            });
        }
        //组织机构
        if (abp.auth.hasPermission('Pages.Administration.OrganizationUnits')) {
            $stateProvider.state('organizationUnits', {
                url: '/organizationUnits',
                templateUrl: '~/App/common/views/organizationUnits/index.cshtml',
                menu: 'Administration.OrganizationUnits'
            });
        }
        //通知
        $stateProvider.state('notifications', {
            url: '/notifications',
            templateUrl: '~/App/common/views/notifications/index.cshtml'
        });

        //HOST routes
        $stateProvider.state('host', {
            'abstract': true,
            url: '/host',
            template: '<div ui-view class="fade-in-up"></div>'
        });
        //租户
        if (abp.auth.hasPermission('Pages.Tenants')) {
            //$urlRouterProvider.otherwise("/host/tenants"); //Entrance page for the host（站点入口地址）
            $stateProvider.state('host.tenants', {
                url: '/tenants?filterText',
                templateUrl: '~/App/host/views/tenants/index.cshtml',
                menu: 'Tenants'
            });
        }
        //版本列表
        if (abp.auth.hasPermission('Pages.Editions')) {
            $stateProvider.state('host.editions', {
                url: '/editions',
                templateUrl: '~/App/host/views/editions/index.cshtml',
                menu: 'Editions'
            });
        }
        //？？？
        if (abp.auth.hasPermission('Pages.Administration.Host.Maintenance')) {
            $stateProvider.state('host.maintenance', {
                url: '/maintenance',
                templateUrl: '~/App/host/views/maintenance/index.cshtml',
                menu: 'Administration.Maintenance'
            });
        }
        //？？？
        if (abp.auth.hasPermission('Pages.Administration.Host.Settings')) {
            $stateProvider.state('host.settings', {
                url: '/settings',
                templateUrl: '~/App/host/views/settings/index.cshtml',
                menu: 'Administration.Settings.Host'
            });
        }

        //TENANT routes
        //？？？
        $stateProvider.state('tenant', {
            'abstract': true,
            url: '/tenant',
            template: '<div ui-view class="fade-in-up"></div>'
        });
        //工作台
        if (abp.auth.hasPermission('Pages.Tenant.Dashboard')) {
            $urlRouterProvider.otherwise("/tenant/dashboard"); //Entrance page for a tenant（站点入口地址）
            $stateProvider.state('tenant.dashboard', {
                url: '/dashboard',
                templateUrl: '~/App/tenant/views/dashboard/index.cshtml',
                menu: 'Dashboard.Tenant'
            });
        }
        //设置
        if (abp.auth.hasPermission('Pages.Administration.Tenant.Settings')) {
            $stateProvider.state('tenant.settings', {
                url: '/settings',
                templateUrl: '~/App/tenant/views/settings/index.cshtml',
                menu: 'Administration.Settings.Tenant'
            });
        }
        //稿件状态
        if (abp.auth.hasPermission('Pages.Tenant.ManuscriptStatus')) {
            $stateProvider.state('tenant.manuscriptstatus', {
                url: '/manuscriptstatus',
                templateUrl: '~/App/tenant/views/manuscriptstatus/index.cshtml',
                menu: 'ManuscriptStatus'
            });
        }

        ////转签稿件
        //if (abp.auth.hasPermission('Pages.Tenant.ManuscriptChecked')) {
        //    $stateProvider.state('tenant.manuscriptchecked', {
        //        url: '/manuscriptchecked',
        //        templateUrl: '~/App/tenant/views/manuscriptchecked/index.cshtml',
        //        menu: 'ManuscriptChecked'
        //    });
        //}

        //稿件信息
        if (abp.auth.hasPermission('Pages.Tenant.Manuscript')) {
            $stateProvider.state('tenant.manuscript', {
                url: '/manuscript',
                templateUrl: '~/App/tenant/views/manuscript/index.cshtml',
                menu: 'Manuscript'
            });
        }
        //类型管理（件类型、栏目类型、公告类型）
        if (abp.auth.hasPermission('Pages.Tenant.ParameterType')) {
            $stateProvider.state('tenant.parametertype', {
                url: '/parametertype',
                templateUrl: '~/App/tenant/views/parametertype/index.cshtml',
                menu: 'ParameterType'
            });
        }

        ////作家用户信息
        //if (abp.auth.hasPermission('Pages.Tenant.WriterInformation')) {
        //    $stateProvider.state('tenant.writerinformation', {
        //        url: '/writerinformation',
        //        templateUrl: '~/App/tenant/views/writerinformation/index.cshtml',
        //        menu: 'WriterInformation'
        //    });
        //}

        //须知信息
        if (abp.auth.hasPermission('Pages.Tenant.Instructions')) {
            $stateProvider.state('tenant.instructions', {
                url: '/instructions',
                templateUrl: '~/App/tenant/views/instructions/index.cshtml',
                menu: 'Instructions'
            });
        }

        //文件
        //if (abp.auth.hasPermission('Pages.Tenant.Files')) {
        //    $stateProvider.state('tenant.files', {
        //        url: '/files',
        //        templateUrl: '~/App/tenant/views/files/index.cshtml',
        //        menu: 'Files'
        //    });
        //}

        //审核意见
        if (abp.auth.hasPermission('Pages.Tenant.Audit')) {
            $stateProvider.state('tenant.audit', {
                url: '/audit',
                templateUrl: '~/App/tenant/views/audit/index.cshtml',
                menu: 'Audit'
            });
        }

        ////公告中间表
        //if (abp.auth.hasPermission('Pages.Tenant.AnnouncementContact')) {
        //    $stateProvider.state('tenant.announcementcontact', {
        //        url: '/announcementcontact',
        //        templateUrl: '~/App/tenant/views/announcementcontact/index.cshtml',
        //        menu: 'AnnouncementContact'
        //    });
        //}

        //公告信息
        if (abp.auth.hasPermission('Pages.Tenant.Announcement')) {
            $stateProvider.state('tenant.announcement', {
                url: '/announcement',
                templateUrl: '~/App/tenant/views/announcement/index.cshtml',
                menu: 'Announcement'
            });
        }
    }
]);

appModule.run(["$rootScope", "settings", "$state", 'i18nService', function ($rootScope, settings, $state, i18nService) {
    $rootScope.$state = $state;
    $rootScope.$settings = settings;

    //Set Ui-Grid language
    if (i18nService.get(abp.localization.currentCulture.name)) {
        i18nService.setCurrentLang(abp.localization.currentCulture.name);
    } else {
        i18nService.setCurrentLang("en");
    }

    $rootScope.safeApply = function (fn) {
        var phase = this.$root.$$phase;
        if (phase == '$apply' || phase == '$digest') {
            if (fn && (typeof (fn) === 'function')) {
                fn();
            }
        } else {
            this.$apply(fn);
        }
    };
}]);